package work.chenxr.rabbitmq.receiver;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author Chenxr
 * @Date 2023/10/18
 * @Description 队列消息接收器
 */
@Slf4j
@Component
public class MqMessageReceiver {
    @RabbitListener(queues = "${app.rabbitmq.delay.chenxr.queue}")
    private void consumer(String chenxrId, Channel channel, Message message) throws IOException {
        log.info("消费者接收到延迟消息[{}]", chenxrId);
        try {
            // 业务逻辑
            log.info("业务逻辑处理中...");
            log.info("业务逻辑处理完毕！");
        } catch (Exception e) {
            // 若处理失败，重新投递至队列中
            // ...
            throw new RuntimeException(e);
        }
        // 签收消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
